﻿/**
* @author	:com.nickro
* @USAGE	:单一加载，一般用于加载，然后将加载内容添加到其他DisplayObjectContainer.
	<code>
		var sLoader:SingleLoader = new SingleLoader();
		sLoader.loadSwf('a.swf');
		sLoader.addEventListener(LoadEvent.PROGRESS,FUNCTION);
		sLoader.addEventListener(LoadEvent.COMPLETE,FUNCTION);
	</code>
*/

package org.nickro.project.load{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	
	import org.nickro.project.events.LoadEvent;
	import org.nickro.project.Config;
	import org.nickro.project.debug.Console;
	public class SingleLoader extends Sprite
	{
		private var _url:String ;
		private var loader:Loader;

		public function SingleLoader()
		{
			/**
			 * 可链接到库中的加载元件,基类org.nickro.project.load.SingleLoader
			 */
		}
		/**
		 * 加载函数
		 * @param  url		加载文件URL地址
		 */
		public function loadSwf(url:String):void
		{
			_url = url;
			loader = new Loader();
			start();
		}
		
		/**
		 *	停止加载操作
		 */
		public function stop():void
		{
			deleteEventListener(loader.contentLoaderInfo);
			loader.close();
		}
		
		/**
		 * 恢复加载操作
		 */
		public function start():void
		{
			loader.load(new URLRequest(_url));
			configEventListener(loader.contentLoaderInfo);
		}
		
		public function die():void
		{
			deleteEventListener(loader.contentLoaderInfo);
		}
		
		private function configEventListener(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(ProgressEvent.PROGRESS,onProgress);
			dispatcher.addEventListener(Event.COMPLETE,onComplete);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecError);
		}
		
		private function deleteEventListener(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(ProgressEvent.PROGRESS,onProgress);
			dispatcher.removeEventListener(Event.COMPLETE,onComplete);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
			dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecError);
		}
		
		private function onProgress(e:ProgressEvent):void
		{
			var percent:int = int((e.bytesLoaded/e.bytesTotal)*100);
			dispatchEvent(new LoadEvent(LoadEvent.PROGRESS,percent));
		}
		private function onComplete(e:Event):void
		{
			var content:* = e.target.content;
			
			if(_contain){
				if(this.numChildren > 0)
				{
					removeChildAt(0);
				}
				addChild(content);
			}
			dispatchEvent(new LoadEvent(LoadEvent.COMPLETE,content));
			deleteEventListener(e.currentTarget as LoaderInfo);
			
		}
		
		private function onIOError(e:IOErrorEvent):void
		{
			throw new IOError('"' + _url + '"'+ " is not existed");
		}
		
		private function onSecError(e:SecurityErrorEvent):void
		{
			throw new SecurityError(this + "安全问题");
		}
		/**
		* Flag to addChild 'content' in this or not
		* @param   value 	Boolean   Flag to addChild 'content' in this or not .Default Value :: true
 		*/
		private var _contain:Boolean = true;
		public function set contain(value:Boolean):void
		{
			_contain = value;
		}
		//
		public function get url():String { return _url; }
		
		public var loadIndex:int;
	}
}